What is dot-prop?
The dot-prop npm package allows for getting, setting, and deleting properties from objects using dot-path notation. This can be particularly useful for accessing deeply nested properties without having to check each level of the object structure.
What are dot-prop's main functionalities?
Get a property
Retrieve a property from an object by specifying its path as a string.
const dotProp = require('dot-prop');
const object = {foo: {bar: 'a'}};
console.log(dotProp.get(object, 'foo.bar')); //=> 'a'
Set a property
Set a property on an object at a specified path, creating any necessary objects along the path.
const dotProp = require('dot-prop');
let object = {foo: {bar: 'a'}};
dotProp.set(object, 'foo.bar', 'b');
console.log(object.foo.bar); //=> 'b'
Delete a property
Delete a property from an object at a specified path.
const dotProp = require('dot-prop');
let object = {foo: {bar: 'a'}};
dotProp.delete(object, 'foo.bar');
console.log(object); //=> { foo: {} }
Check if an object has a property
Check if an object has a property at a specified path.
const dotProp = require('dot-prop');
const object = {foo: {bar: 'a'}};
console.log(dotProp.has(object, 'foo.bar')); //=> true
Other packages similar to dot-prop
lodash.get
Similar to the 'get' functionality of dot-prop, lodash.get allows accessing a property value of an object using a dot-path. While lodash offers a broader utility toolkit, dot-prop focuses specifically on dot-path property manipulation.
object-path
object-path provides similar functionality to dot-prop for getting, setting, and deleting properties using a dot-path notation. It offers a similar API but might have different performance characteristics or additional minor features.
dot-prop
Get, set, or delete a property from a nested object using a dot path
Install
$ npm install --save dot-prop
Usage
const dotProp = require('dot-prop');
dotProp.get({foo: {bar: 'unicorn'}}, 'foo.bar');
dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep');
dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep', 'default value');
dotProp.get({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\.dot');
const obj = {foo: {bar: 'a'}};
dotProp.set(obj, 'foo.bar', 'b');
console.log(obj);
const foo = dotProp.set({}, 'foo.bar', 'c');
console.log(foo);
dotProp.set(obj, 'foo.baz', 'x');
console.log(obj);
dotProp.has({foo: {bar: 'unicorn'}}, 'foo.bar');
const obj = {foo: {bar: 'a'}};
dotProp.delete(obj, 'foo.bar');
console.log(obj);
obj.foo.bar = {x: 'y', y: 'x'};
dotProp.delete(obj, 'foo.bar.x');
console.log(obj);
API
get(obj, path, [defaultValue])
set(obj, path, value)
Returns the object.
has(obj, path)
delete(obj, path)
obj
Type: Object
Object to get, set, or delete the path
value.
path
Type: string
Path of the property in the object, using .
to separate each nested key.
Use \\.
if you have a .
in the key.
The following path components are invalid and results in undefined
being returned: __proto__
, prototype
, constructor
.
value
Type: any
Value to set at path
.
defaultValue
Type: any
Default value.
License
MIT © Sindre Sorhus